<?php
class filesModel extends baseModel{
    public $table = 'cms_files'; 
    //上传成功的文件写入数据库
    public function saveFile($fileinfo){
        $str = strlen(ROOT_PATH);
        $data['name']=$fileinfo['savename'];
        $data['type']=$fileinfo['extension'];
        $data['path']=$fileinfo['savepath'];
        $data['uptime']=time();
        $rel = $this->insert($data);
        if($rel){
            return TRUE;
        }else{
            return FALSE;
        }
    }
    //删除上传文件
    public function DeleteFile($fid){
        $condition['id'] = $fid;
        $rel = $this->find($condition);
        if($rel){
            $path = $rel['path'];
            $ImagesPath = ROOT_PATH.$path.$rel['name'];
            if(file_exists($ImagesPath)){
                if(!unlink($ImagesPath)){
                    return FALSE;
                }else{
                    $this->delete($condition);
                    return TRUE;
                }
            }else{
                $this->delete($condition);
                return TRUE;
            } 
        }
        return FALSE;
    }
    //附件上传
    /*
    //上传成功返回的信息
        Array ([0] => Array (
        [name] => 未命名.jpg
        [type] => image/pjpeg
        [size] =>53241
        [key] => Filedata
        [extension] =>jpg
        [savepath] => ../upload/2011-12-17/
        [savename] => name.jpg )
    )
    */
    public function uploadFile($files){
        if(empty($files)){
            return false;
        }
        $uppath = config('CMS_UPFOLDER');
        $upload_exts = config('CMS_UPEXTS'); //类型
        $upload_max = intval(config('CMS_UPSIZE')); //大小
        $upload = new UploadFile();  //上传类
        $upload->maxSize = 1024 * 1024 * $upload_max; //大小
        $upload->allowExts = explode(',',$upload_exts);  //允许上传的文件的类型
        $upload->savePath = $uppath.'/public/'.date('Y-m-d').'/';  //设置文件上传目录的路径
        $upload->saveRule = cp_uniqid; //上传文件名
        if ($upload->upload()){ //取得成功上传的文件信息
            return $upload->getUploadFileInfo();
        }else{
            return $upload->getErrorMsg();
        }
    } 
    //缩略图的生成
    public function imagesthumb($imgurl,$w,$h){
        $thumbpath = config('CMS_UPFOLDER').'thumb/'.date('Y-m-d').'/';
        $images = ROOT_PATH.$imgurl;
        $name = cp_uniqid().'.jpg'; //文件名
        $thumbname = $thumbpath.'/'.$name; //文件名
        $rel = Image::thumb($images,$thumbname,$type,$w,$h,$interlace=true);
        if($rel){
            $data['savename']=$name;
            $data['extension']='jpg';
            $data['savepath']=$thumbpath;
            model('files')->saveFile($data);
            return TRUE;
        }else{
            return FALSE;
        }
    }
}